package com.atguigu.gulimall.cart.controller;

import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.Cart;
import com.atguigu.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItems(){

        return cartService.getUserCartItems();
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuid") Long skuid,
                            @RequestParam("check") Integer check){
        cartService.checkItem(skuid,check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuid") Long skuid,
                            @RequestParam("num") Integer num){
        cartService.changeItemCount(skuid,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuid") Long skuid){
        cartService.deleteItem(skuid);
        return "redirect:http://cart.gulimall.com/cart.html";
    }



    /**
     * 浏览器有一个cookie: user-key: 用来标示用户身份，一个月过后过期
     * 如果第一次使用jd，都会给一个临时用户身份，浏览器以后保存，以后每次访问都带上这个cookie
     *
     * 登录：session里有
     * 没登录：按照cookie里带来的user-key来做
     * 第一次如果没有临时用户，帮忙创建一个临时用户
     *
     * 获取购物车
     * @param //session
     * @return
     */
    @GetMapping("/cart.html")
    public String  cartListPage(Model model) throws ExecutionException, InterruptedException {

//        //快速得到用户信息id,user-key
//        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
//        System.out.println(userInfoTo);
        Cart cart =cartService.getCart();
        //System.out.println(cart.getTotalAmount());
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * ra.addFlashAttribute() 模拟session的方式,将数据放在session中,在页面可以取出,但是只可取一次
     * ra.addAttribute("skuId",skuId);  以参数的方式放在url后面
     * 添加商品到购物车
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes ra) throws ExecutionException, InterruptedException {
        //CartItem cartItem = cartService.addToCart(skuId,num);
        //model.addAttribute("item",cartItem);
        cartService.addToCart(skuId,num);
        ra.addAttribute("skuId",skuId);

        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * 解决重复提交的问题,
     * 跳转到成功页
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,
                                       Model model){
        //重定向到成功页面后，再次查询购物车的数据即可
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItem);
        return "success";
    }




}
